package Processes;

import threadedSimulator.Cpu;

/**
 * a consumer process.
 * @author Harris
 */
public class ConsumerProcess extends Processes{

    /**
     * the cpu we are using to spawn a producer.
     */
    Cpu my_cpu;
    
    /**
     * the value from the producer.
     */
    double my_producer_value;
    
    /**
     * checks to see if producer is done. 
     */
    boolean producer_is_finished = false;
    
    /**
     * constructor
     * @param the_cpu the cpu
     * @param the_numberof_pro_con the number of producers and consumers.
     */
    public ConsumerProcess(Cpu the_cpu, String the_numberof_pro_con) {
	super(the_cpu, the_numberof_pro_con, "Consumer");
	my_priority = 2;
        my_cpu = the_cpu;
    }

    /**
     * runs the consumer process.
     */
    @Override
    public void run() {
        if(can_run)
        {
        	my_cpu.addProcess(new ProducerProcess(my_cpu, "", this)); //spawns a new proccess
        	can_run = false;
        }
        if (producer_is_finished) {
            can_run = true;
        }
        while (can_run && running && my_current_index <= 10 && producer_is_finished) {
            running = true;
            my_producer_value = my_producer_value + 6;
            my_current_index++;
            if (my_current_index == 10) {//restarts the consumer loop
                my_current_index = 0;
                running = true;
                my_producer_value = 0;
                producer_is_finished = false;
                can_run = true;
            }	
            
        }
        
    }
	
}
